#include "ScheduleCountDown.h"

using namespace cocos2d;
ScheduleCountDown::ScheduleCountDown()
{
}

ScheduleCountDown::~ScheduleCountDown()
{
}

bool ScheduleCountDown::init(ScheduleCounterDelegate* target,int perimeter/*=60*/,bool loop/*=true*/)
{
	_target=target;
	_maxTime=perimeter;
	_curTime=_maxTime;
	_loop=loop;
	schedule(schedule_selector(ScheduleCountDown::schedulePerSecond),1.0f);
	return true;
}

ScheduleCountDown* ScheduleCountDown::create(ScheduleCounterDelegate* target,int perimeter/*=60*/,bool loop/*=true*/)
{
	ScheduleCountDown* pRet=new ScheduleCountDown;
	pRet->init(target,perimeter,loop);
	pRet->autorelease();
	return pRet;
}

void ScheduleCountDown::schedulePerSecond(float delta)
{
	_curTime--;
	if(_curTime <= 0){
		if(getLoop()){
			_curTime = _maxTime;
		}else{
			unschedule(schedule_selector(ScheduleCountDown::schedulePerSecond));
		}
		_target->scheduleTimeUp();
	}
	_target->setScheduleNumber(_curTime);
}
